package com.medical.controller;

import java.math.BigInteger;
import java.security.MessageDigest;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import org.json.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import com.medical.HttpTools.HttpRequest;

@Controller  
//@RequestMapping(value = "/web", method = {RequestMethod.GET, RequestMethod.POST}) 
@RequestMapping(value="/Login")
/**
 * 
 * @author LTT-AH-ZY
 * @date 创建时间：2017年7月24日 下午5:07:18 
 * @version 1.0
 * @parameter 参数及其意义
 * @return 返回值
 * @throws 异常类及抛出条件
 */
public class LoginController {
	/**
	 * 初始化登录界面
	 * @return
	 */
	@RequestMapping(value = "/login")  
    public ModelAndView login(ModelAndView model) {  
		model.setViewName("LoginView/Login");
        return model; 
	}
	/**
	 * 医院端登陆界面的简单注册提交类，
	 * @param hosplogininfo
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/register")
	public @ResponseBody Map<String,Object> register(HttpServletRequest request) throws Exception {
		String hosploginphone = request.getParameter("hosploginphone");
		String hosploginpwd = request.getParameter("hosploginpwd");
		String code = request.getParameter("code");
		
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(hosploginpwd.getBytes());
			String hosploginpwdMD5 = new BigInteger(1, md.digest()).toString(16);
			System.out.println("输出MD5加密后的密码是："+hosploginpwdMD5);
			String param = "hosploginphone="+hosploginphone+"&hosploginpwd="+hosploginpwdMD5+"&code="+code;
			try {
				JSONObject json = HttpRequest.sendPost("register", param);
				String jsoncode = json.get("code").toString();
				String jsonmsg = json.get("msg").toString();
				
				result.put("code", jsoncode);
				result.put("msg", jsonmsg);
			} catch (Exception e) {
				System.out.println("出错了，输出错误信息是："+e.getMessage());
				result.put("code", -100);
				result.put("msg", "数据解析出错，注册失败！");
			}
		} catch (Exception e) {
			System.out.println("MD5加密出错了，输出错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "密码加密出错了，注册失败！");
		}
		return result;
	}
	/**
	 * 简单注册界面的发送手机验证码，发送前先检测手机号码是否已经注册过！
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/SendPhoneCodeR")
	public @ResponseBody Map<String,Object> SendPhoneCodeR(HttpServletRequest request)throws Exception{
		String hosploginphone = request.getParameter("hosploginphone");
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			JSONObject phonejson = HttpRequest.sendPost("phonetest", "hosploginphone="+hosploginphone);
			String phonejsoncode = phonejson.get("code").toString();
			String phonejsonmsg = phonejson.get("msg").toString();
			System.out.println("输出jsoncode:"+phonejsoncode);
			if("100".equals(phonejsoncode)) {
				try {
					 JSONObject PWDjson = HttpRequest.sendPost("getmsgcode", "hosploginphone="+hosploginphone);
					 String jsoncode = PWDjson.get("code").toString();
					 String jsonmsg = PWDjson.get("msg").toString();
					 
					 result.put("code", jsoncode);
					 result.put("msg", jsonmsg);
				} catch (Exception e) {
					System.out.println("解析出错了！错误信息是："+e.getMessage());
					result.put("code", -100);
					result.put("msg", "数据解析失败！");
				}
			}else {
				result.put("code", phonejsoncode);
				result.put("msg", phonejsonmsg);
			}
		} catch (Exception e) {
			System.out.println("解析出错了！错误信息是："+e.getMessage());
			result.put("code", -300);
			result.put("msg", "检测手机是否注册时失败！");
		}
		return result;
	}
	
	/**
	 * 医院端登陆实现submit，对传输的密码进行MD5加密再通过http传输到服务器，实现用户名和密码的验证！
	 * 实现登陆的验证
	 * @param hosplogininfoCustom
	 * @param session
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/loginsubmit")
	public @ResponseBody Map<String,Object> loginsubmit(HttpServletRequest request,HttpSession session)throws Exception{
		System.out.println("进入登陆提交函数！！");
		String hosploginphone = request.getParameter("hosploginphone");
		String hosploginpwd = request.getParameter("hosploginpwd");
		Map<String,Object> result = new HashMap<String,Object>();
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(hosploginpwd.getBytes());
			String hosploginpwdMD5 = new BigInteger(1, md.digest()).toString(16);
			String param = "hosploginphone="+hosploginphone+"&hosploginpwd="+hosploginpwdMD5;
			try {
				JSONObject json = HttpRequest.sendPost("login", param);
				System.out.println("输出这个json是："+json);
				String logincode = json.get("code").toString();
				String loginmsg = json.get("msg").toString();
				if("100".equals(logincode)) {
					JSONObject datajson = json.getJSONObject("data");
					String huanxinaccount = datajson.get("huanxinaccount").toString();
					Integer hosploginid=(Integer) datajson.get("hosploginid");
					String hosploginpix=datajson.get("hosploginpix").toString();
					session.setAttribute("huanxinaccount", huanxinaccount);
					session.setAttribute("hosploginid", hosploginid);
					session.setAttribute("hosploginphone", hosploginphone);
					session.setAttribute("hosploginpix", hosploginpix);
					/*try {
						JSONObject loginjson = HttpRequest.sendPost("weblogin", "huanxinaccount="+huanxinaccount);
						String jsoncode = loginjson.get("code").toString();
						String jsonmsg = loginjson.get("msg").toString();
						System.out.println("输出code："+jsoncode+"&msg="+jsonmsg);
					} catch (Exception e) {
						System.out.println("出错了，输出错误信息是："+e.getMessage());
					}*/
				}
				result.put("code", logincode);
				result.put("msg", loginmsg);
			} catch (Exception e) {
				System.out.println("出错了1，出错信息是："+e.getMessage());
				result.put("code", -100);
				result.put("msg", "数据解析出错了，登陆失败！");
			}
		} catch (Exception e) {
			System.out.println("出错了2，出错信息是："+e.getMessage());
			result.put("code", -200);
			result.put("msg", "密码加密出错了，登陆失败！");
		}
		System.out.println("输出这个result="+result);
		return result;
	}
	/**
	 * 登出类，销毁session中的数据，实现登出
	 * @param session
	 * @return
	 */
	//登出函数
	@RequestMapping(value="/logoutsubmit")
	public String logoutsubmit(HttpSession session){
		session.invalidate();
		return "redirect:login.action";
	}
	/**
	 * 忘记密码，输入新密码和注册的手机号码和验证码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/forgivePWD")
	public @ResponseBody Map<String,Object> forgivePWD(HttpServletRequest request)throws Exception{
		String hosploginphone = request.getParameter("hosploginphone");
		String hosploginpwd = request.getParameter("hosploginpwd");
		String code = request.getParameter("code");
		Map<String,Object> result = new HashMap<String,Object>();
		//对接收的密码进行MD5加密
		try {
			MessageDigest md = MessageDigest.getInstance("MD5");
			md.update(hosploginpwd.getBytes());
			String hosploginpwdMD5 = new BigInteger(1, md.digest()).toString(16);
			System.out.println("输出MD5加密后的密码是："+hosploginpwdMD5);
			String param = "hosploginphone="+hosploginphone+"&hosploginpwd="+hosploginpwdMD5+"&code="+code;
			try {
				JSONObject PWDjson = HttpRequest.sendPost("editpassword", param);
				String jsoncode = PWDjson.get("code").toString();
				String jsonmsg = PWDjson.get("msg").toString();
				
				result.put("code", jsoncode);
				result.put("msg", jsonmsg);
			} catch (Exception e) {
				System.out.println("出错了，输出错误信息是："+e.getMessage());
				result.put("code", -100);
				result.put("msg", "数据解析出错，修改密码失败！");
			}
		} catch (Exception e) {
			System.out.println("MD5加密出错了，输出错误信息是："+e.getMessage());
			result.put("code", -200);
			result.put("msg", "密码加密出错了，修改密码失败！");
		}
		return result;
	}
	/**
	 * 忘记密码页面发送手机验证码
	 * @param request
	 * @return
	 * @throws Exception
	 */
	@RequestMapping(value="/SendPhoneCodeF")
	public @ResponseBody Map<String,Object> SendPhoneCodeF(HttpServletRequest request)throws Exception{
		String hosploginphone = request.getParameter("hosploginphone");
		Map<String,Object> result = new HashMap<String,Object>();
		 try {
			 JSONObject PWDjson = HttpRequest.sendPost("getmsgcode", "hosploginphone="+hosploginphone);
			 String jsoncode = PWDjson.get("code").toString();
			 String jsonmsg = PWDjson.get("msg").toString();
				
			 result.put("code", jsoncode);
			 result.put("msg", jsonmsg);
		} catch (Exception e) {
			System.out.println("解析出错了！错误信息是："+e.getMessage());
			result.put("code", -100);
			result.put("msg", "数据解析失败！");
		}
		return result; 
	}
}